home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 48 < prev    next >
Internet Message Format  |  1996-08-06  |  2KB

  1. Path: news.cs.ucla.edu!twinsun!not-for-mail
  2. From: eggert@twinsun.com (Paul Eggert)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Undefined result vs. int's holding undefined values.
  5. Date: 7 Jan 1996 18:20:39 -0800
  6. Organization: Twin Sun Inc, El Segundo, CA, USA
  7. Message-ID: <4cpv1n$apm@der.twinsun.com>
  8. References: <4ck70b$rd7@news.informix.com> <4ckms5$rd7@news.informix.com>  <4cmg0s$1mb@der.twinsun.com> <oZA8wQ9ytpjN084yn@csn.net>
  9. NNTP-Posting-Host: der.twinsun.com
  10.  
  11. thads@csn.net (Thad Smith) writes:
  12.  
  13. > it is possible to rewrite the function so that it doesn't
  14. > invoke undefined behavior
  15.  
  16. Yes.  But it slows things down measurably for an interpreter to use
  17. such a clumsy method for integer overflow detection.  The (x + y < x)
  18. != (y < 0) method requires a comparison, an exclusive OR of the
  19. comparison result bit with a sign bit, and a conditional branch.
  20. The method you suggested requires a comparison, a conditional branch, a
  21. subtraction, another comparison, and another conditional branch.
  22.  
  23. Since this is comp.std.c, I'll mention that the (x + y < x) != (y < 0) method
  24. and Daniel Wood's example both port to any C implementation
  25. claiming conformance to LIA-1 (ISO/IEC 10967-1:1994(E),
  26. `Information technology -- Language independent arithmetic -- Part 1:
  27. Integer floating point arithmetic' <ftp://crl.dec.com/pub/misc/lia-1-dis.ps.Z>)
  28. if INT_MODULO is true (which is the only plausible value for INT_MODULO
  29. for real-world C compilers).  I doubt whether Wood's compiler vendor
  30. claims LIA-1 conformance today, but this may become more important in
  31. the future, and perhaps Wood can point his vendor at LIA-1 and say that
  32. it's time to read the handwriting on the wall.
  33.